<template>
  <el-dialog :title="title" center :visible.sync="dialogFormVisible">
    <!--    查询条件部分-->
    <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">查询条件</span></el-divider>
    <div>
      <el-form ref="showForm" label-position="right" label-width="100px">

        <el-row :gutter="24">
          <el-col :span="7">
            <el-form-item label="楼号信息：">
              <el-input v-model="queryParma.lhxx" clearable size="small" placeholder="请输入查询信息" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否有效：">
              <el-select v-model="queryParma.sfyxbs" style="position: relative;left: 5px" filterable default-first-option placeholder="请选择有效标识">
                <el-option
                  key="0"
                  label=""
                  value=""
                />
                <el-option
                  key="1"
                  label="是"
                  value="1"
                />
                <el-option
                  key="2"
                  label="否"
                  value="0"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="隶属区县："
            >
              <el-input :value="hasSelectedCotyObj.value" placeholder="请选择隶属区县" autocomplete="off" clearable @click.native="showCotyDialog" />
              <el-dialog
                :visible.sync="dialogCotyVisible"
                append-to-body
              >
                <el-input
                  v-model="filterText"
                  placeholder="输入关键字进行查询"
                />
                <div class="down-tree">
                  <el-tree
                    ref="tree"
                    class="filter-tree"
                    :data="dictsInfoB"
                    :props="defaultProps"
                    default-expand-all
                    :filter-node-method="filterNode"
                    @node-click="handleNodeClick"
                  />
                </div>
                <div slot="footer" class="dialog-footer">
                  <el-button @click="handleCancel">清 空</el-button>
                  <el-button type="primary" @click="handleConfirmCoty">确 定</el-button>
                </div>
              </el-dialog>
            </el-form-item>
          </el-col>
          <el-col :span="5" align="right">
            <el-button type="primary" :loading="searchloding" @click="getBuildingInfoListPageHandle">查询</el-button>
            <el-button @click="resetForm('showForm')">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!--    查询查询部分-->
    <div>
      <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">楼号信息展示</span></el-divider>
      <!--      数据操作按钮-->
      <div style="padding: 10px" align="right">
        <el-button-group>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
          <el-button type="danger" icon="el-icon-delete" @click="handleDeleteBatch">批量删除</el-button>
        </el-button-group>
      </div>

      <el-table
        v-loading="loading"
        :data="dataSource"
        :border="true"
        align="center"
        empty-text="暂无数据"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        />
        <el-table-column
          type="index"
          :index="indexMethod"
          label="#"
          width="50"
          align="center"
        />
        <el-table-column
          label="楼号"
          align="center"
          prop="buildingNumber"
        />
        <el-table-column
          label="小区名称"
          prop="villageName"
          min-width="100"
        />
        <el-table-column
          label="隶属管辖区域"
          min-width="100"
          header-align="center"
          align="center"
          prop="cotyCode"
        >
          <template slot-scope="{row}">
            {{ getDictsValue(row.cotyCode) }}
          </template>
        </el-table-column>
        <el-table-column
          label="楼号注册时间"
          prop="createTime"
          header-align="center"
          min-width="150"
        />
        <el-table-column
          label="楼号更新时间"
          prop="updateTime"
          header-align="center"
          min-width="150"
        />
        <el-table-column
          label="是否有效"
          header-align="center"
          align="center"
          min-width="120"
          prop="valiFlag"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.valiFlag"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="switchHandle(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          min-width="150"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div align="center">
        <div style="display: inline-block;width: 60px;height:60px;line-height: 60px;text-align: center;font-size: 15px">
          <span style="text-align: center;line-height: 60px;width: 60px;height:60px;">共{{ dataTotals }}条</span>
        </div>
        <div style="display: inline-block">
          <el-pagination
            :current-page.sync="queryParma.pageIndex"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="queryParma.pageSize"
            layout="total, sizes, prev,next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <build-modal
      ref="buildmodals"
      :building-info="modifyData"
      :village-code="queryParma.xqbh"
      @reflushDataHandle="getBuildingInfoListPageHandle"
    />
  </el-dialog>
</template>

<script>
const { getAllBuildingInfoPageFilter, dropBatchBuildingInfo, modifyOneBuildingInfo } = require('@/api/ht/xqzhgl/lhgl/lhxxgl')
const { getDictsList, getParentDictsList } = require('@/api/dicts')
const { Message } = require('element-ui')
import BuildModal from '@/views/ht/xqzhgl/xqxxgl/components/buildModal'
export default {
  name: 'Index',
  components: {
    BuildModal
  },
  props: {
    dictsInfoB: {
      type: Array,
      default: () => []
    },
    alldictsInfoB: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      title: '',
      filterText: '',
      dataSource: [], // 楼号信息列表
      loading: false, // 数据加载提示
      dataTotals: 0, // 查询总条数
      modifyData: {}, // 待修改对象信息
      dialogCotyVisible: false, // 是否展示区县选择对话框
      searchloding: false, // 搜索按钮加载
      dialogFormVisible: false, // 是否展示本对话框
      hasSelectedCotyObj: {}, // 选中的区县对象信息
      queryParma: {
        pageIndex: 1,
        pageSize: 5,
        xqbh: '',
        sfyxbs: '',
        qxbh: '',
        lhxx: ''
      },
      hasselectedIdArray: [], // 已经选中的信息ID数组
      defaultProps: {
        children: 'children',
        label: 'value'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  mounted() {
    this.getBuildingInfoListPageHandle() // 获取楼号信息
  },
  methods: {
    getDictsValue(code) {
      const parentStr = getParentDictsList(this.dictsInfoB, code)
      const kidStr = getDictsList(this.alldictsInfoB, code)
      const resultStr = parentStr + ' - ' + kidStr
      return resultStr
    },
    filterNode(value, data) {
      if (!value) return true
      return data.value.indexOf(value) !== -1
    },
    showDialog(row) {
      this.queryParma.xqbh = row.id
      this.title = '小区名称：' + row.villageName
      this.getBuildingInfoListPageHandle() // 如果是通过点击主页的楼号管理进入的 则需手动一次查询对应楼号信息方法
      this.dialogFormVisible = true
    },
    handleConfirmCoty() {
      this.queryParma.qxbh = this.hasSelectedCotyObj.code
      this.dialogCotyVisible = false
    },
    handleNodeClick(clickobj, Node, Tree) {
      // 点击父节点不进行操作
      if (clickobj.children) { return null }
      // 点击子节点进行逻辑操作
      this.hasSelectedCotyObj = clickobj
    },
    getBuildingInfoListPageHandle() {
      this.getBuildingInfoListPage()
    },
    async getBuildingInfoListPage() {
      this.loading = true
      const res = await getAllBuildingInfoPageFilter(this.queryParma)
      const data = res.data.main.dataArray
      const resp = res.data.respOutMsgHeader
      const totals = res.data.main.totalsData
      const currentPage = res.data.main.currentPage
      if (resp.respCode === 200) {
        setTimeout(() => {
          this.searchloding = false
          this.loading = false
        }, 500)
        this.dataSource = data
        this.dataTotals = totals
        this.queryParma.pageIndex = currentPage
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.loading = false
      }
    },
    indexMethod(index) {
      return index + 1 + (this.queryParma.pageIndex - 1) * this.queryParma.pageSize
    },
    handleCancel() {
      this.queryParma.qxbh = ''
      this.hasSelectedCotyObj = {}
      this.dialogCotyVisible = false
    },
    handleAdd() {
      this.$refs.buildmodals.checkModelType('add')
    },
    handleEdit(index, row) {
      // 使用父子传参方式
      this.modifyData = row
      this.$refs.buildmodals.checkModelType('edit')
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleDelete(index, row) {
      const rowIdarray = []
      rowIdarray.push(row.id)
      this.dropOneBuildingInfo(rowIdarray)
    },
    handleDeleteBatch() {
      this.dropBatchBuildinginfo()
    },
    async dropOneBuildingInfo(array) {
      const res = await dropBatchBuildingInfo(array)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.getBuildingInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async dropBatchBuildinginfo() {
      const res = await dropBatchBuildingInfo(this.hasselectedIdArray)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.getBuildingInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    showCotyDialog() {
      this.dialogCotyVisible = true
    },
    handleSizeChange(val) {
      this.queryParma.pageSize = val
      this.getBuildingInfoListPageHandle()
    },
    handleCurrentChange(val) {
      this.queryParma.pageIndex = val
      this.getBuildingInfoListPageHandle()
    },
    handleSelectionChange(selections) {
      const rowIdArray = []
      for (const selection of selections) {
        rowIdArray.push(selection.id)
      }
      this.hasselectedIdArray = rowIdArray
    },
    switchHandle(row) {
      this.$confirm('如果设置无效操作，将会使得该小区关联的绝大部分功能失效或者无法正常使用系统, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.modifyBuildingInfo(row)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
      this.getBuildingInfoListPageHandle()
    },
    async modifyBuildingInfo(row) {
      const res = await modifyOneBuildingInfo(row)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: '设置成功' || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.getBuildingInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    }
  }
}
</script>

<style scoped>
.down-tree{
  height: 350px;
  display: block;
  overflow-y: scroll;
}
</style>
